package com.amapv2.apis.basic;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CoordinateConverter;
import com.amap.api.maps.MapView;
import com.amapv2.apis.R;
import com.amapv2.apis.util.LocationUtils;

/**
 * AMapV2地图中介绍如何显示一个基本地图
 * 1、mapView.onCreate(savedInstanceState);// 此方法必须重写
 * 2、生命周期的方法必须重写
 */
public class LocationActivvity extends Activity {

    private static final String TAG = LocationActivvity.class.getSimpleName();
    private Context mContext;//上下文
    private MapView mapView;//Map的View
    private AMap aMap;//AMap对象
    private TextView mTvLatLong;//存放经纬度
    private TextView mTvCity;//存放城市
    private TextView mTvIsInChina;//是否在中国

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_activity);

        initParamsAndValus();

        initView(savedInstanceState);

        initMap();

        initData();
    }

    private void initParamsAndValus() {
        mContext = this;
    }

    private void initView(Bundle savedInstanceState) {
        mTvLatLong = (TextView) findViewById(R.id.tv_lat_long);
        mTvCity = (TextView) findViewById(R.id.tv_city);
        mTvIsInChina = (TextView) findViewById(R.id.tv_is_inchina);

        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
    }

    /**
     * 初始化AMap对象
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    private void initData() {
        requestLocationInfo();
    }

    /**
     * 请求定位的信息
     */
    private void requestLocationInfo() {
        LocationUtils.getLocation(mContext, new LocationUtils.LocationResult() {
            @Override
            public void value(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {//0表示成功
                        //1、获得维度，经度，城市
                        double latitude = amapLocation.getLatitude();
                        double longitude = amapLocation.getLongitude();
                        String city = amapLocation.getCity();

                        //2、设置维度，经度，城市
                        mTvLatLong.setText("维度：" + latitude + " 经度：" + longitude);
                        mTvCity.setText(city);

                        //3、请求是否在中国
                        requestIsInChina(latitude, longitude);
                    }
                }
            }
        });
    }

    /**
     * 请求是否在中国
     *
     * @param latitude
     * @param longitude
     */
    private void requestIsInChina(double latitude, double longitude) {
        //1、获得是否在中国地区，返回true代表当前位置在大陆、港澳地区，反之不在。
        CoordinateConverter converter = new CoordinateConverter(mContext);
        boolean isAMapDataAvailable = converter.isAMapDataAvailable(latitude, longitude);

        //2、设置是否在中国地区
        mTvIsInChina.setText("是否在中国:" + isAMapDataAvailable);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

}
